class MachinesController < ApplicationController
  
    #GET /machines
    #GET /machines.xml
    before_filter :ensure_login
    require 'socket'
    require 'ping'
    
    include Socket::Constants
    
    def index
    	@machines = Machine.all
    	respond_to do |format|
    		format.html #index.html.erb
    		format.xml { render :xml =>@machines }
    	end		
    end
    
    #GET /machines/1
    #GET /machines/1.xml
    def show
    	@machine= Machine.find(params[:id])
    	respond_to do |format|
    		format.html #show.html.erb
    		format.xml { render :xml => @machine }
    	end
    end
    
    #GET /machines/new
    #GET /machines/new.xml
    def new
    	@machine = Machine.new
    	respond_to do |format|
    		format.html	#new.html.erb
    		format.xml { render :xml => @machine }
    	end	
    end
    
     def new_from_view
      @rooms = Room.all
      @parametre = params[:id]
      @machine = Machine.new
      respond_to do |format|
        format.html #new.html.erb
        format.xml { render :xml => @machine }
      end 
    end
    
    # GET /machines/1/edit
    def edit
    	@machine = Machine.find(params[:id])
    end
    
    # POST /machines
    # POST /machines.xml
    def create
    	@machine = Machine.new(params[:machine])
    	respond_to do |format|
    		if @machine.save
    			flash[:notice] = 'Machine was successfully created'
    			format.html { redirect_to(@machine)}
    			format.xml { render :xml => @machine, :status => :created, :location => @machine }
    		else
    			format.html { render :action => "new" }
    			format.xml { render :xml => @machine.errors, :status => :unprocessable_entity }
    		end	
    	end
    end
    
    # PUT /machines/1
    # PUT /machines/1.xml
    
    def update
    	@machine = Machine.find(params[:id])
    	respond_to do |format|
    		if @machine.update_attributes(params[:machine])
      		flash[:notice] = 'Machine was successfully updated.'
      		format.html { redirect_to(@machine) }
      		format.xml { head :ok }
    		else
      		format.html { render :action => "edit" }
      		format.xml { render :xml => @machine.errors, :status => :unprocessable_entity }	
    		end	
    	end
    end
    
    def incidentlist        
      
        @Incidents = Incident.paginate(:page => params[:page], :conditions => ["resolution_date IS NULL and machine_id = ?", params[:id]])
        
    end
    
    def destroy
    	@machine = Machine.find(params[:id])
    	@machine.destroy
		Incident.delete_all("machine_id = #{params[:id]}")
    	
    	respond_to do |format|
    	format.html { redirect_to(machines_url) }
    	format.xml {head :ok}
    	end
    end
    

  
end


